<?php

namespace app\api\controller;

use think\Db;

use app\common\controller\Logined;
use app\common\service\TranslateService;

class Balance extends Logined{

public function getRecord(){
		$uid        = $this->uid;
		$page       = P('page')?:1;
		$group      = P('group');
		$limit      = P('limit')?:10;
		$start_time = P('start_time');
		$end_time   = P('end_time');
		$wid        = P('wid');
		$type       = P('type');
		$inorto     = P('inorto');
		$where      = ' 1=1';
		if($group != ''){
			$where .= ' and b.type>='.$group.' and b.type<'.($group+10);
		}
		if($wid != '') {
            $where .= ' and b.wid='.$wid;
        }
		if($type != '') {

            $where .= ' and b.type in('.$type.')';
        }
        if($inorto != '') {
        	if ($inorto  == 1) {
        		$where .= ' and b.balance>0';
        	} else if ($inorto  == 2) {
        		$where .= ' and b.balance<0';
        	}
        }
		// if($start_time != '') {
  //       	$start_time = strtotime($start_time);
  //           $where .= ' and b.date>='.$start_time;
  //       }
		// if($end_time != '') {
		// 	$end_time = strtotime($end_time);
  //           $where .= ' and b.date<='.$end_time;
  //	}
		if(empty($start_time)){
			$start_time = 0;
		} else {
			$start_time = strtotime($start_time);
		}
		if(empty($end_time)){
			$end_time = time();
		} else{
			$end_time = strtotime($end_time)+86400;
		}
		$data = Db::table('tp_balance_record')->alias('b')
											->join('tp_coin_wallet w','w.id=b.wid')
											->join('tp_balance_record_type t','t.type=b.type')
											->where('b.date','>=',$start_time)
											->where('b.date','<=',$end_time)
											->where('b.uid',$uid)
											->where($where)
											->field('b.*,w.name wallet_name,t.remark type_name')
											->fetchSql(false)
											->page($page)
											->limit($limit)
											->order('b.id desc')
											->select();
		if($data){
			foreach ($data as $key => $val) {
				$data[$key]['balance'] = $val['balance']/1;
				$data[$key]['remark'] = TranslateService::baiduApi($val['remark'],$this->lang);
				// $data[$key]['order_remark'] = TranslateService::baiduApi($val['order_remark'],$this->lang);
			}
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0,'fail');
		}
	}

	//资金记录分组
	public function recordGroup(){
		$data = Db::table('tp_balance_record_type')->where('status',1)->grouP('group')->field('group,group_name')->select();
		//翻译
		foreach ($data as $key => $val) {
			$data[$key]['group_name'] = TranslateService::baiduApi($val['group_name'],$this->lang);
		}
		$this->retData(0,'success',$data);
	}
}